package com.cardosystems.infra.pages;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class CardoCommunitySiteFrame extends AbstractPage {

	@FindBy(xpath = "//a[@href=\"members/home\"]")
	WebElement homeLink;

	@FindBy(xpath = "//a[@href=\"pages/settings-and-radio\"]")
	WebElement settingsLink;
	
	@FindBy(xpath = "//a[@href=\"/logout\"]")
	WebElement logoutLink;
	
	@FindBy(xpath = "//*[@class=\"edit-profile-link button-base\"]")
	WebElement editProfile;

	@FindBy(id = "voice_tag_p")
	WebElement deviceStatus;

	@FindBy(id = "device_sync")
	WebElement syncBtn;

	
	
	public CardoCommunitySiteFrame(WebDriver driver) {
		super(driver);
		PageFactory.initElements(driver, this);
		waitForDevice();
	}

	@Override
	public boolean isInModule() {
		return false;
	}

	public CardoCommunityHomePage navigateToHomePage() throws Exception {
		homeLink.click();
		return new CardoCommunityHomePage(driver);
	}

	public CardoCommunitySettingsPage navigateToSettingPage() throws Exception {
		settingsLink.click();
		return new CardoCommunitySettingsPage(driver);
	}
	
	public UserRegistrationPage logout() throws Exception{
		logoutLink.click();
		return new UserRegistrationPage(driver);
	}

	public CardoCommunityEditProfilePage clickEditProfile() throws Exception {
		editProfile.click();
		return new CardoCommunityEditProfilePage(driver);
	}

	public boolean ClickOnSyncBtn() throws Exception {
		// set the implicit wait to 10 sec.
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		boolean isSync = false;
		WebElement progressStatus;
		// validate that the sync button is "red"
		// if red
		if (syncBtn.getAttribute("class").equals("red blinker")) {
			// click on sync button
			syncBtn.click();
			while (!isSync) {
				try {
					Thread.sleep(1000);
					// get the progress status div, if the div will not be found
					// in 5 sec exception will be thrown
					progressStatus = driver.findElement(By
							.xpath("//div[@class=\"progress-status\"]"));
				} catch (Exception e) {
					// the exception means that the progress status div is no
					// longer viable i.e sync is done
					System.out.println("Sync Done");
					isSync = true;
				}
			}
			return true;
		} else {
			// if the sync button is not red
			System.out.println("Can not Sync");
			return false;
		}
	}

	private void waitForDevice() {
		WebDriverWait wait = new WebDriverWait(driver, 5);
		Boolean element;
		try {
			System.out.println("Wating to reading from device");
			element = wait.until(ExpectedConditions.textToBePresentInElement(
					By.id("voice_tag_p"), "Reading from Device"));
			System.out.println("found reading from device");

		} catch (TimeoutException e) {
			element = false;
		}
		System.out
				.println("Wating to reading from device to end and removal of the overly div");

		if (element
				&& !deviceStatus.getText().equals("Connect your Device to USB")) {
			wait = new WebDriverWait(driver, 60);
			element = wait.until(ExpectedConditions
					.invisibilityOfElementLocated(By
							.xpath("//div[]@class=\"overlay\"")));
		}
		System.out.println("Overlay is removed");
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
